package com.igeek.blog_parent_test.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.igeek.blog_parent_test.entity.Albums;
import com.igeek.blog_parent_test.mapper.AlbumsMapper;
import com.igeek.blog_parent_test.service.IAlbumsService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.igeek.blog_parent_test.vo.PageQueryVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author author
 * @since 2024-12-04
 */
@Service
public class AlbumsServiceImpl extends ServiceImpl<AlbumsMapper, Albums> implements IAlbumsService {
    @Autowired
    private AlbumsMapper albumsMapper;
    //分页条件查询相册
    @Transactional(readOnly = true)
    @Override
    public Page<Albums> findPage(PageQueryVO vo) {
        Integer currentPage = vo.getCurrentPage();
        Integer pageSize = vo.getPageSize();
        Page<Albums> page = Page.of(currentPage, pageSize);

        String queryString = vo.getQueryString();
        LambdaQueryWrapper<Albums> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(StringUtils.hasLength(queryString),Albums::getAlbumId,queryString)
                .or()
                .like(StringUtils.hasLength(queryString),Albums::getName,queryString);

        Page<Albums> albumsPage = this.baseMapper.selectPage(page,lambdaQueryWrapper);
        return albumsPage;
    }

    //删除相册
    @Transactional
    @Override
    public boolean del(Integer id) {
        //相册被占用，抛出自定义异常

        //未被占用，直接删除
        int i = this.baseMapper.deleteById(id);
        return i>0?true:false;
    }
}
